<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if(in_right_admin('UserScoreLevel/add')) btns['添加'] = function(){ app.iframe('edit') }
    if(in_right_admin('UserScoreLevel/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    if(this.admin_config('this_tenant.id')==0 && in_right_admin('Tenant/copy_data')) btns['同步数据'] = function(){
      if (!app.$refs['list_url'].selected.length) return app.tips("请先选择需要同步的任务", 2);
      (new Promise((resolve)=>{
        app.iframe_select('/tenant/index', function(data){
          if (!data.length) return app.tips("选择的租户不能为空", 2);
          let tenant_ids = [];
          data.forEach((v)=>{ tenant_ids.push(v.id); })
          resolve(tenant_ids.join(','))
          return true;
        })
      })).then((tenant_ids)=>{
        app.$refs['list_url'].ids_action("", "/admin/tenant/copy_data", function (res) {
          app.tips(`共同步了 ${res.data} 条数据`, 1);
          return false;
        }, {action_label:"同步", data:{tenant_ids:tenant_ids, table:'user_score_level'}})
      })
    }
    let forms_config = {
      // create_time: {label:'创建时间',name:'where[create_time][between]',config:'Y-M-D',type:'date_between',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type:'hidden', order:true},
      level:{name:'level', label:'段位', width:'60px'},
      head_img: {name:'head_img', label:'段位图片', width:'60px', type:'file_img'},
      title:{name:'title', label:'段位名称', width:'120px'},
      min_score:{name:'min_score', label:'达成积分', width:'80px'},
      score_type:{name:'score_type', label:'积分类型', type:'radio_switch', config:'总积分|all,剩余积分|surplus', width:'120px', can_edit:true, onchange:this.change_type},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/user_score_level/'+(is_del?'recycle':'list'),
        title: "积分段位列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },

    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('UserScoreLevel/edit') && ~[0,row.tenant_id].indexOf(this.admin_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+s94Mjs.param_encode(param))
    },
    edit_level(row){
      this.$prompt('输入新的段位序列，当前段位：'+row.level,'修改段位').then((data)=>{
        ajax({url:'/admin/user_score_level/edit_level?id='+row.id, data:{level:data.value}}, (res)=>{
          if (res.data>0) this.$refs['list_url'].list();
        })
      }).catch(()=>{})
    },
    change_type(row, col, v){
      let app = this;
      this.confirm("是否确定修改积分类型？", function (){
        ajax({url: '/admin/user_score_level/edit_score_type?id=' + row.id, data: {score_type: v}}, (res) => {
          if (res.data > 0) app.$refs['list_url'].list();
        })
      })
      return false;
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

  }
}
</script>
